package com.niren.relay.netty;

import com.niren.relay.common.constant.constant;
import com.niren.relay.common.devinfo.GetDevState;
import com.niren.relay.common.devinfo.SetDevState;
import com.niren.relay.common.portocol.AT.AtCmdGenerater;
import com.niren.relay.redis.RedisString;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

public class SendMssage implements Runnable {
    TcpServerHandler tcpServer = null;
    public SendMssage(TcpServerHandler tcpServer){
        this.tcpServer = tcpServer;
    }

    @Override
    public void run() {
        //当多台设备时，这里的发送速度非常慢，请自行修改逻辑
        System.out.println("SendMssage Thread Start!");
        while(true){
            try {
                Thread.sleep(100);//50毫秒，减少粘包次数
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            RedisString rString = new RedisString(constant.hostOrIp);
            String macs = rString.getValue(constant.macsKey);

            int getQueueCnt = constant.getStateQueue.GetQueueCount();
            if(getQueueCnt > 0){//发送非控制指令
                GetDevState getDevState = constant.getStateQueue.OutQueue();
                String msg = getDevState.getCmd();
                String remoteAddress = tcpServer.getRemoteAddress(getDevState.getCtx());
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())
                        + " -->  向" + remoteAddress + "发送指令：" + msg);
                getDevState.getCtx().channel().writeAndFlush(Unpooled.copiedBuffer(msg , CharsetUtil.UTF_8));
                continue;
            }

            int queueCnt = constant.setStateQueue.GetQueueCount();
            if(queueCnt == 0) continue;

            SetDevState devState = constant.setStateQueue.OutQueue();
            String mac = devState.getMacOrKey();
            String ch = "0";
            Integer state = devState.getState();

            if(devState.getControlRange() == 0){
                writeData2Device(mac,ch,state);
            }else if(devState.getControlRange() == 1){
                if(mac.contains("|") == true){
                    String[] macArr = mac.split("\\|");
                    mac = macArr[0];
                    ch = macArr[2];
                }
                writeData2Device(mac,ch,state);
            }
        }
    }

    private void writeData2Device(String mac,String ch,int state){
        String msg = AtCmdGenerater.GetCtlCmd(Integer.parseInt(ch) ,state);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())
                + " -->  向 " + mac + " 发送指令：" + msg);
        tcpServer.macMap.get(mac).getCtx().channel().writeAndFlush(Unpooled.copiedBuffer(msg , CharsetUtil.UTF_8));//ctx.channel().writeAndFlush(Unpooled.copiedBuffer(msg , CharsetUtil.UTF_8));
        tcpServer.UpdateOutState(tcpServer.getRemoteAddress(tcpServer.macMap.get(mac).getCtx()));

        GetDevState getDevState = new GetDevState();
        getDevState.setCtx(tcpServer.macMap.get(mac).getCtx());
        getDevState.setCmd(AtCmdGenerater.GetOutStateCmd(Integer.parseInt(ch)));
        constant.getStateQueue.InQueue(getDevState);
    }


}
